/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormLogin.java
 *
 * Created on Nov 18, 2012, 4:49:21 PM
 */
package siabukukaskoperasi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import tools.koneksi;

/**
 *
 * @author sepen
 */
public class FormLogin extends javax.swing.JFrame {

    Login lg = new Login();
    String getUname, getPass;

    /**
     * Creates new form FormLogin
     */
    public FormLogin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        BtnLogin = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        LblJudul1 = new javax.swing.JLabel();
        LblJudul2 = new javax.swing.JLabel();
        LabelSilahkan = new javax.swing.JLabel();
        LabelUsername = new javax.swing.JLabel();
        LabelPassword = new javax.swing.JLabel();
        jTfUsername = new javax.swing.JTextField();
        Jpassword = new javax.swing.JPasswordField();

        jPasswordField1.setText("jPasswordField1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(204, 255, 51));

        BtnLogin.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        BtnLogin.setText("Login");
        BtnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLoginActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(102, 0, 204));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Logo-Koperasi.gif"))); // NOI18N
        jLabel4.setText("jLabel1");

        LblJudul1.setBackground(new java.awt.Color(204, 255, 51));
        LblJudul1.setFont(new java.awt.Font("Californian FB", 1, 36)); // NOI18N
        LblJudul1.setForeground(new java.awt.Color(51, 51, 51));
        LblJudul1.setText("Selamat Datang di SIA Buku");
        LblJudul1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        LblJudul2.setBackground(new java.awt.Color(204, 255, 51));
        LblJudul2.setFont(new java.awt.Font("Californian FB", 1, 36)); // NOI18N
        LblJudul2.setForeground(new java.awt.Color(51, 51, 51));
        LblJudul2.setText("Kas Koperasi Kredit Karya Jasa");
        LblJudul2.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LblJudul2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(LblJudul1)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(LblJudul1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LblJudul2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        LabelSilahkan.setFont(new java.awt.Font("Tahoma", 0, 14));
        LabelSilahkan.setText("Silahkan Login terlebih dahulu :");

        LabelUsername.setFont(new java.awt.Font("Tahoma", 0, 14));
        LabelUsername.setText("Username             :");

        LabelPassword.setFont(new java.awt.Font("Tahoma", 0, 14));
        LabelPassword.setText("Password              :");

        jTfUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTfUsernameActionPerformed(evt);
            }
        });

        Jpassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JpasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(379, 379, 379)
                        .addComponent(BtnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(LabelPassword)
                            .addComponent(LabelUsername))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTfUsername, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Jpassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(LabelSilahkan)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(LabelSilahkan)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LabelUsername))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelPassword)
                    .addComponent(Jpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addComponent(BtnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLoginActionPerformed
        try {

            Connection konek = koneksi.getDBConnection();
            getUname = jTfUsername.getText();
            getPass = Jpassword.getText();
            String query = "select username, password, status from LOGIN where USERNAME='" + getUname + "' and PASSWORD=" + getPass;
            Statement state = (Statement) koneksi.getDBConnection().createStatement();
            ResultSet rs = state.executeQuery(query);
            String uName = "";
            String pass = "";
            String stat = "";
            while (rs.next()) {
                uName = rs.getString(1);
                pass = rs.getString(2);
                stat = rs.getString(3);

            }
            // System.out.println(getUname + " " + getPass + " " + uName + " " + pass );


            if (uName.equals(getUname) && pass.equals(getPass)) {
//                JOptionPane.showMessageDialog(null, "username atau password yang anda masukkan salah");

                FormUtama utama = new FormUtama();
//            utama.setStatus(stat);
                if (stat.equalsIgnoreCase("bendahara")) {
                    utama.getBtnPemasukan().setEnabled(false);
                } else if (stat.equalsIgnoreCase("teller")) {
                    utama.getBtnPengeluaran().setEnabled(false);
                }

                utama.setVisible(true);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "username dan password salah");
            }


//            FormUtama utama = new FormUtama();
////            utama.setStatus(stat);
//            if (stat.equalsIgnoreCase("bendahara")) {
//                utama.getBtnPemasukan().setEnabled(false);
//            } else if (stat.equalsIgnoreCase("teller")) {
//                utama.getBtnPengeluaran().setEnabled(false);
//            }
//
//            utama.setVisible(true);
//            this.dispose();

        } catch (SQLException ex) {
//            JOptionPane.showMessageDialog(null, "username atau password yang anda masukkan salah");
            Logger.getLogger(FormLogin.class.getName()).log(Level.SEVERE, null, ex);
        }

}//GEN-LAST:event_BtnLoginActionPerformed

private void jTfUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTfUsernameActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jTfUsernameActionPerformed

private void JpasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JpasswordActionPerformed
    // TODO add your handling code here:
    try {

        Connection konek = koneksi.getDBConnection();
        getUname = jTfUsername.getText();
        getPass = Jpassword.getText();
        String query = "select username, password, status from LOGIN where USERNAME='" + getUname + "' and PASSWORD=" + getPass;
        Statement state = (Statement) koneksi.getDBConnection().createStatement();
        ResultSet rs = state.executeQuery(query);
        String uName = "";
        String pass = "";
        String stat = "";
        while (rs.next()) {
            uName = rs.getString(1);
            pass = rs.getString(2);
            stat = rs.getString(3);

        }
        // System.out.println(getUname + " " + getPass + " " + uName + " " + pass );


        if (uName.equals(getUname) && pass.equals(getPass)) {
//                JOptionPane.showMessageDialog(null, "username atau password yang anda masukkan salah");

            FormUtama utama = new FormUtama();
//            utama.setStatus(stat);
            if (stat.equalsIgnoreCase("bendahara")) {
                utama.getBtnPemasukan().setEnabled(false);
            } else if (stat.equalsIgnoreCase("teller")) {
                utama.getBtnPengeluaran().setEnabled(false);
            }

            utama.setVisible(true);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "username dan password salah");
        }

    } catch (SQLException ex) {
//        JOptionPane.showMessageDialog(null, "username atau password yang anda masukkan salah");
        Logger.getLogger(FormLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_JpasswordActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnLogin;
    private javax.swing.JPasswordField Jpassword;
    private javax.swing.JLabel LabelPassword;
    private javax.swing.JLabel LabelSilahkan;
    private javax.swing.JLabel LabelUsername;
    private javax.swing.JLabel LblJudul1;
    private javax.swing.JLabel LblJudul2;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTfUsername;
    // End of variables declaration//GEN-END:variables
}
